﻿namespace Microsoft.SharePoint.Client.Workflow
{
    using Microsoft.SharePoint.Client;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    [ScriptType("SP.Workflow.WorkflowAssociationCollection", ServerTypeId="{4e1196ea-ce71-4aa9-b3c0-3f8da05deec9}")]
    public class WorkflowAssociationCollection : ClientObjectCollection<WorkflowAssociation>
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public WorkflowAssociationCollection(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public WorkflowAssociation Add(WorkflowAssociationCreationInformation parameters)
        {
            if (base.Context.ValidateOnClient && (parameters == null))
            {
                throw ClientUtility.CreateArgumentNullException("parameters");
            }
            WorkflowAssociation association = new WorkflowAssociation(base.Context, new ObjectPathMethod(base.Context, base.Path, "Add", new object[] { parameters }));
            association.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(association.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, association);
            base.Context.AddQuery(query);
            base.AddChild(association);
            association.InitFromCreationInformation(parameters);
            return association;
        }

        [Remote]
        public virtual WorkflowAssociation GetById(Guid associationId)
        {
            return new WorkflowAssociation(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetById", new object[] { associationId }));
        }

        [Remote]
        public WorkflowAssociation GetByName(string name)
        {
            Dictionary<string, WorkflowAssociation> dictionary;
            object obj2;
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetByName", out obj2))
            {
                dictionary = (Dictionary<string, WorkflowAssociation>) obj2;
            }
            else
            {
                dictionary = new Dictionary<string, WorkflowAssociation>();
                base.ObjectData.MethodReturnObjects["GetByName"] = dictionary;
            }
            WorkflowAssociation association = null;
            if (!dictionary.TryGetValue(name, out association))
            {
                association = new WorkflowAssociation(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetByName", new object[] { name }));
                dictionary[name] = association;
                ObjectIdentityQuery query = new ObjectIdentityQuery(association.Path);
                base.Context.AddQueryIdAndResultObject(query.Id, association);
                base.Context.AddQuery(query);
            }
            return association;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (flag)
            {
                return flag;
            }
            return flag;
        }
    }
}

